package com.ding.controller;


import com.ding.drools.KieTemplate;
import com.ding.entity.People;
import com.ding.entity.RuleInfo;
import com.ding.service.RuleService;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ding
 * @date 2020/10/20 17:28
 */

@RestController
@RequestMapping("/")
@Slf4j
public class TestController {

    @Autowired
    private KieTemplate kieTemplate;

    @Autowired
    private RuleService ruleService;

    @GetMapping("/prople")
    public String test(@RequestParam("name") String name, @RequestParam("sex") Integer sex) {
        People people = new People();
        people.setSex(sex);
        people.setName(name);
//        String drl = ruleService.getRuleById(61L);
        List<RuleInfo> drlList = ruleService.getAllRules();
//        KieHelper kieHelper = new KieHelper();
//        drlList.forEach(
//                d -> kieHelper.addContent(d.getContent(), ResourceType.DRL)
//        );
//        Results verify = kieHelper.verify();
//        log.info("{}",verify.getMessages());
        String[] strings = new String[drlList.size()];
        for (int i = 0; i < drlList.size(); i++) {
            strings[i] = drlList.get(i).getContent();
        }
        KieSession session = kieTemplate.decodeToSession(strings);
        session.insert(people);
        int i = session.fireAllRules();
        System.out.println("i = " + i);
        session.dispose();
        return people.toString();
    }

    @GetMapping
    public void demo() {

    }

}
